from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from rest_framework.views import APIView
from rest_framework.generics import ListAPIView
from apps.meiduo_admin.serializers.user import UserSerializer
from apps.users.models import User
from rest_framework.pagination import LimitOffsetPagination,PageNumberPagination
from rest_framework.response import Response
from collections import OrderedDict


class PageNum(PageNumberPagination):

    # http://127.0.0.1:8000/meiduo_admin/users/?page=1&pagesize=10&keyword=
    # 前端的pagesize=10 写死了
    # 如果前端传递了 pagesize=10 就不会用默认的值了
    # 开启分页,设置默认每页多少条记录.如果不传递 page_size 这个属性起作用
    page_size = 5

    # 必须设置为 pagesize 前端写死了
    page_size_query_param = 'pagesize'

    # 最大记录数.防止人为攻击. 一下获取1亿条,我们不能真给1亿条数据
    max_page_size = 50

    def get_paginated_response(self, data):
        """
         "count": "用户总量",
        "lists": [
            {
                "id": "用户id",
                "username": "用户名",
                "mobile": "手机号",
                "email": "邮箱"
            },
            ...
        ],
        "page": "页码",
        "pages": "总页数",
        "pagesize": "页容量"
        """
        return Response(OrderedDict([
            ('count', self.page.paginator.count),            # 总共记录条数
            ('lists', data),                                 # 结果类别
            ('page', self.page.number),                      # 当前页码
            ('pages', self.page.paginator.num_pages),        # 总页数
            ('pagesize', self.page_size),                    # 一页多少条记录
        ]))


# class PageNum(PageNumberPagination):
#     page_size = 5
#     page_size_query_param = 'pagesize'
#     max_page_size = 50
#
#     def get_paginated_response(self, data):
#         return Response(OrderedDict([
#             ('count', self.page.paginator.count),
#             ('lists', data),
#             ('page', self.page.number),
#             ('pages', self.page.paginator.num_pages),
#             ('pagesize', self.page_size)
#         ]))


class UserAPIView(ListModelMixin, CreateModelMixin, GenericAPIView):
    # 查询结果集
    # queryset = User.objects.all()
    """
    我们需要根据 不同的需求,返回不同的查询结果集
    就需要重写方法

    queryset-应该用于从该视图返回对象的查询集。
    通常，您必须设置此属性或重写get_queryset()方法
    """
    def get_queryset(self):
        # meiduo_admin/users/?keyword=<搜索内容>&page=<页码>&pagesize=<页容量>
        # 获取查询结果集的数据
        keyword = self.request.query_params.get('keyword')
        # 需求
        if keyword:
            # 有筛选
            # or
            return User.objects.filter(username__contains=keyword)
        else:
            # 没有筛选
            return User.objects.all()

    # 设置序列化器类
    serializer_class = UserSerializer
    # 设置分页类
    pagination_class = PageNum

    def get(self, request):
        # 1.查询所有数据
        # 2.将所有数据通过序列化器转换为字典数据
        # 3.返回响应
        return self.list(request)

    def post(self, request):

        return self.create(request)


# class UserAPIView(ListModelMixin, CreateModelMixin, GenericAPIView):
#
#     def get_queryset(self):
#         keyword = self.request.query_params.get('keyword')
#         if keyword:
#             return User.objects.filter(username__contains=keyword)
#         else:
#             return User.objects.all()
#
#     serializer_class = UserSerializer
#     pagination_class = PageNum
#
#     def get(self, request):
#         return self.list(request)
#
#     def post(self, request):
#         return self.create(request)

class User2APIView(ListAPIView):
    # 查询结果集
    queryset = User.objects.all()
    # 设置序列化器类
    serializer_class = UserSerializer



